//JavaScript Framework 2.0 Code
try {
	Type.registerNamespace('com.yonyou.ncmm_pac_pda.SaleOutScanListController');
	com.yonyou.ncmm_pac_pda.SaleOutScanListController = function() {
		com.yonyou.ncmm_pac_pda.SaleOutScanListController.initializeBase(this);
		this.initialize();
	}

	function com$yonyou$ncmm_pac_pda$SaleOutScanListController$initialize() {}

	function com$yonyou$ncmm_pac_pda$SaleOutScanListController$evaljs(js) {
		eval(js)
	}

	//返回按钮
	function com$yonyou$ncmm_pac_pda$SaleOutScanListController$back(sender, args) {
		$view.close({
			"resultCode": "15"
		});
	}

	//右上角公共菜单
	function loadmenu(sender, args) {
		NCMMPubMenu.openDropDownList("imagebutton0");
	}

	// 列表行单击事件 -- 弹出浮动按钮组
	function onitemclick(sender, args) {
		//如果点击的是组列表，则不弹出浮动按钮组
		var row = {};
		row = $id("listviewdefine0").get("row");
		row = $stringToJSON(row);
		if(row && row["row"])
			return;

		$view.openPop(NCMMSALEOUTUtil.popDialogInfo("panel35"));
	}

	//浮动按钮组 -- 详情
	function onclick_Detail() {
		//先关闭按钮组
		onclick_Cancel();

		// 获取当前行内容
		var row = {};
		row = $id("listviewdefine0").get("row");

		// 单击行跳转到详情页面
		row = $stringToJSON(row);
		$view.open({
			"viewid": "com.yonyou.ncmm_pac_pda.SaleOutScanDetail",
			"isKeep": "true",
			"row": row
		});

	}

	//浮动按钮组 -- 修改
	function onclick_Modify() {
		//先关闭按钮组
		onclick_Cancel();

		// 获取当前行内容
		var rowData = $id("listviewdefine0").get("row");
		rowData = $stringToJSON(rowData);

		var bodydata = {};
		bodydata[SaleOutScansVO.VBILLCODE.name] = rowData[SaleOutScansVO.VBILLCODE.name];
		bodydata[SaleOutScansVO.CROWNO.name] = rowData[SaleOutScansVO.CROWNO.name];
		bodydata[SaleOutScansVO.TS.name] = rowData[SaleOutScansVO.TS.name];

		var row = {
			"data": rowData,
			"type": "4" //图4 --> 图3 （修改）
		};
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.SaleOutScan",
			isKeep: "false",
			row: row
		});

	}

	//浮动按钮组 -- 删除
	function onclick_Delete() {
		//先关闭按钮组
		onclick_Cancel();

		//询问是否删除
		var params = {
			"title": "扫描记录",
			"message": "确定删除当前扫描记录吗？",
			"okbuttontitle": "确定",
			"cancelbuttontitle": "取消",
			"style": "ok-cancel",
			"okaction": "doDelete()",
			"cancelaction": "doCancel()"
		};
		$view.openDialog(params);
	}

	//浮动按钮组 -- 取消
	function onclick_Cancel() {
		// 取消
		$view.closePop(NCMMSALEOUTUtil.popDialogInfo("panel35"));
	}

	//删除行 -- 确定
	function doDelete() {
		// 获取当前行内容
		var scanvo = {};
		scanvo = $id("listviewdefine0").get("row");
		scanvo = $stringToJSON(scanvo);

		//删除扫描记录表当前行
		var condition = SaleOutScansVO.VBILLCODE.name + "='" + scanvo[SaleOutVO.VBILLCODE.name] +
			"' and " + SaleOutScansVO.CROWNO.name + "= '" + scanvo[SaleOutVO.CROWNO.name] +
			"' and " + SaleOutScansVO.TS.name + "= '" + scanvo[SaleOutScansVO.TS.name] + "'";

		NCMMPubUtil.batchDeleteByCondition(SaleOutScansVO, condition);

		//更新上游单据表
		NCMMSALEOUTUtil.updateSaleOutRecord(scanvo[SaleOutVO.VBILLCODE.name], scanvo[SaleOutVO.CROWNO.name]);

		//更新界面显示
		loadAllScanRecords();

	}

	//删除行 -- 取消
	function doCancel() {
		$toast("删除操作已取消。");
	}

	//组checkbox 单击事件
	function onClickGroup(sender, args) {
		$ctx.dataCollect();
		var isCheckedAll = true;

		var sourceData = $ctx.getJSONArray("source");
		for(var i = 0; i < sourceData.length; i++) {
			if(sourceData[i]["isgroupchecked"] == null || sourceData[i]["isgroupchecked"] == false || sourceData[i]["isgroupchecked"] == "false") {
				isCheckedAll = false;
				for(var j = 0; j < (sourceData[i]["row"]).length; j++) {
					((sourceData[i]["row"])[j])["ischecked"] = false;
				}
			} else {
				for(var j = 0; j < (sourceData[i]["row"]).length; j++) {
					((sourceData[i]["row"])[j])["ischecked"] = true;
				}
			}
		}
		$ctx.put("isAllChecked", isCheckedAll); //更新全选复选框的界面显示
		$ctx.put("source", sourceData);
		$ctx.dataBind();
	}

	//列表行checkbox 单击事件（暂时不支持）
	function onClickItem(sender, args) {
		//组内条目复选框只能看不能操作

	}

	//全选checkbox 单击事件
	function onAllClick(sender, args) {
		$ctx.dataCollect();
		var isChecked = $id(sender).get("checked");
		var sourceData = $ctx.getJSONArray("source");
		for(var i = 0; i < sourceData.length; i++) {
			sourceData[i]["isgroupchecked"] = isChecked;

			for(var j = 0; j < (sourceData[i]["row"]).length; j++) {
				((sourceData[i]["row"])[j])["ischecked"] = isChecked;
			}
		}
		$ctx.put("source", sourceData);
		$ctx.dataBind();

	}

	//页面onrestart事件
	function pageRestart(sender, args) {
		loadAllScanRecords();
	}

	//列表加载事件
	function com$yonyou$ncmm_pac_pda$SaleOutScanListController$listviewdefine0_onload(sender, args) {
		loadAllScanRecords();
	}

	//从本地表获取所有材料出库扫描记录并显示
	function loadAllScanRecords() {
		var sql = getAllTableQuerySql();
		var scangroup = getVOsBySql(sql);
		//补齐小数位
		scangroup = NCMMSALEOUTUtil.changeDecimalFiguresByMeascdoc(SaleOutVO, scangroup);

		//显示
		$ctx.push({
			"source": scangroup
		});
	}

	//返回全表查询sql，用单据号+行号 排序
	function getAllTableQuerySql() {
		var sql = "SELECT " +
			SaleOutScansVO.VBILLCODE.name + ", " + SaleOutScansVO.CROWNO.name + ", " +
			SaleOutScansVO.CINVCODE.name + ", " + SaleOutScansVO.CINVNAME.name + ", " +
			SaleOutScansVO.NSHOULDINNUM.name + ", " + SaleOutScansVO.TNAME.name + ", " +
			SaleOutScansVO.NSHOULDASSISTNUM.name + ", " + SaleOutScansVO.FNAME.name + ", " +
			SaleOutScansVO.PKMEASDOC.name + ", " + SaleOutScansVO.CASTUNITID.name +
			", sum(" + SaleOutScansVO.SCANNUM.name + ") as groupsum" +
			" FROM " + SaleOutScansVO.prototype.getTableName() +
			" group by " + SaleOutScansVO.VBILLCODE.name + ", " + SaleOutScansVO.CROWNO.name;

		return sql;
	}

	//根据sql获取本地表vos
	function getVOsBySql(sql) {
		NCMMPubUtil.createTableWithAutoPK(SaleOutScansVO);

		var data = NCMMPubUtil.queryBySQL(sql);
		data = NCMMPubUtil.htmlspecialjsonarray2UI(data);

		for(var i = 0; i < data.length; i++) {
			var vbillcode = data[i][SaleOutScansVO.VBILLCODE.name];
			var crowno = data[i][SaleOutScansVO.CROWNO.name];
			var wheresql = "where " + SaleOutScansVO.VBILLCODE.name + "= '" + vbillcode + "' and " + SaleOutScansVO.CROWNO.name + "= '" + crowno + "'";
			NCMMPubUtil.queryVO(SaleOutScansVO, wheresql, function(subdata) {
				subdata = eval(subdata);
				subdata = NCMMPubUtil.htmlspecialjsonarray2UI(subdata);
				//$alert("subdata.length=" + subdata.length);
				data[i]["row"] = subdata;
			});

		}

		return data;
	}

	//提交事件
	function onSubmit(sender, args) {
		$ctx.dataCollect();
		var isCheckedAll = true;

		var sourceData = $ctx.getJSONArray("source");
		var noRecordCheck = true;
		var outvos = new Array();
		var scanvos = new Array();
		for(var i = 0; i < sourceData.length; i++) {
			if(sourceData[i]["isgroupchecked"] == null || sourceData[i]["isgroupchecked"] == false || sourceData[i]["isgroupchecked"] == "false") {
				continue;
			} else {
				var scanvo = (sourceData[i]["row"])[0];
				var totalnum = NCMMSALEOUTUtil.getSumScannum(scanvo[SaleOutScansVO.VBILLCODE.name], scanvo[SaleOutScansVO.CROWNO.name]);

				//获取当前扫描记录对应的上游单据vo
				var outvo = NCMMSALEOUTUtil.getOriginalOutvo(scanvo[SaleOutScansVO.VBILLCODE.name], scanvo[SaleOutVO.CROWNO.name]);
				if(outvo == null) {
					continue;
				}
				outvos.push(outvo);
				for(var j = 0; j < sourceData[i]["row"].length; j++) {
					scanvo = (sourceData[i]["row"])[j];
					scanvos.push(scanvo);
				}

				noRecordCheck = false;
				//提交数据到NC
				//NCMMSALEOUTUtil.submitInfo2NC(billinfos, outvo, loadAllScanRecords);
			}
		}

		if(noRecordCheck == true) {
			$toast("没有选中扫描记录！");
		}
		//暂时进行如下合并 added by fengjinga
		var allbillinfos = changeVOTOBillInfos(scanvos);
		//提交数据到NC,修改成一起提交
		NCMMSALEOUTUtil.submitInfo2NC(allbillinfos, outvos, loadAllScanRecords);
	}
	// 转换数据
	function changeVOTOBillInfos(vos) {
		if(vos == null || vos.length < 1) {
			return;
		}
		var bpks = [];
		var combinedvos = [];
		// 处理扫描数量，合并vo
		for(var i = 0; i < vos.length; i++) {
			// 来源id+批次号作为分单合并数量维度  //flargess  赠品
			var matchpk = vos[i]["pk_item"] + vos[i]["vbatchcode"] + vos[i]["crowno"] ;

			if(bpks.indexOf(matchpk) == -1) {
				// 行id集合中不存在
				bpks.push(matchpk);
				//处理序列号
				if(NCMMPubUtil.isNotEmpty(vos[i].vserialcode) || NCMMPubUtil.isNotEmpty(vos[i].pk_rack)) {
					vos[i]["bbitems"] = [{
						"vserialcode": vos[i].vserialcode,
						"num": vos[i].scannum,
						"clocationid": vos[i].pk_rack
					}];
				}
				combinedvos.push(vos[i]);

			} else {
				// 如果matchpk在集合中存在，则合并扫描数量
				var currentscanednum = vos[i]["scannum"];
				for(var j = 0; j < combinedvos.length; j++) {
					var combinedpk = combinedvos[j]["pk_item"] + combinedvos[j]["vbatchcode"];
					if(matchpk == combinedpk) {
						//处理序列号
						var bbitems = combinedvos[j]["bbitems"];
						if(bbitems != null && ((vos[i].vserialcode != null && vos[i].vserialcode != "") || (vos[i].pk_rack != null && vos[i].pk_rack != ""))) {
							bbitems.push({
								"vserialcode": vos[i].vserialcode,
								"num": vos[i].scannum,
								"clocationid": vos[i].pk_rack
							});
						}
						var scanednum = combinedvos[j]["scannum"];
						combinedvos[j]["scannum"] = NCMMPubUtil.getSumScanedNum(scanednum, currentscanednum);
						break;
					}
				}
			}
		}
		// 把合并的vo组装成billinfos的格式，用于提交数据
		var billinfos = createBillInfoForScanList(combinedvos);
		return billinfos;
	}

	// 用于上传创建billinfos
	function createBillInfoForScanList(vos) {
		var billinfos = [];
		var pkheads = [];
		// json的key为pkhead,value为items
		var headjson = {};
		var head = {};
		var item = {};
		// 相同的表头合并表体
		for(var i = 0; i < vos.length; i++) {
			var pk_head = vos[i]["pk_head"];
			if(pkheads.indexOf(pk_head) == -1) {
				pkheads.push(pk_head);
				head = getHeadForScanList(vos[i]);
				item = getItemForScanList(vos[i]);
				var items = [];
				items.push(item);
				headjson[pk_head] = items;
			} else {
				item = getItemForScanList(vos[i]);
				var items = headjson[pk_head];
				items.push(item);
				headjson[pk_head] = items;
			}
		}
		// 生成billinfos
		var pks = [];
		for(var j = 0; j < vos.length; j++) {
			var pk_head = vos[j]["pk_head"];
			var pkitems = headjson[pk_head];
			if(pks.indexOf(pk_head) == -1 && !NCMMPubUtil.isEmpty(pkitems)) {
				pks.push(pk_head);
			    head = getHeadForScanList(vos[j]);
				var billinfo = {
					"head": head,
					"items": pkitems
				};
				billinfo = {
					"billinfo": billinfo
				};
				billinfos.push(billinfo);
			}

		}
		return billinfos;
	}

	function getHeadForScanList(vo) {
		var json = {};
		json["pk_head"] = vo.pk_head;
		json["pk_org"] = vo["pk_org_h"];
		json["storcode"] = vo.pk_stordoc;
		return json;
	}

	function getItemForScanList(vo) {
		var item = {};
		item["pk_item"] = vo.pk_item;
		item["pk_material"] = vo.pk_material;
		item["pk_measdoc"] = vo.castunitid;
		item["ts_b"] = vo["btx"];
		item["vbatchcode"] = vo.vbatchcode;
		item["nnum"] = vo["scannum"];
		item["vnotebody"] = vo["vnotebody"];
		//支持序列号
		if(NCMMPubUtil.isNotEmpty(vo.bbitems)) {
			var bbitems = [];
			var bbitem = {};
			for(var i = 0; i < vo.bbitems.length; i++) {
				var bbitem = {};
				bbitem["bbitem"] = {
					"vserialcode": vo.bbitems[i].vserialcode,
					"nnum": vo.bbitems[i].num == "" ? "1.00000" : vo.bbitems[i].num,
					"clocationid": vo.bbitems[i].clocationid
				};
				//bbitem["bbitem"]={"vserialcode":vo.serialno,"nnum":vo.scannum == "" ? "1.00000" : vo.scannum};
				bbitems.push(bbitem);
			}
			item["bbitems"] = bbitems;
		}
		//item["serialno"]=vo.serialno;
		item["nnum"] = vo.scannum == "" ? "1.00000" : vo.scannum;
		item = {
			"item": item
		};
		return item;
	}

	com.yonyou.ncmm_pac_pda.SaleOutScanListController.prototype = {
		listviewdefine0_onload: com$yonyou$ncmm_pac_pda$SaleOutScanListController$listviewdefine0_onload,
		back: com$yonyou$ncmm_pac_pda$SaleOutScanListController$back,
		initialize: com$yonyou$ncmm_pac_pda$SaleOutScanListController$initialize,
		evaljs: com$yonyou$ncmm_pac_pda$SaleOutScanListController$evaljs
	};
	com.yonyou.ncmm_pac_pda.SaleOutScanListController.registerClass('com.yonyou.ncmm_pac_pda.SaleOutScanListController', UMP.UI.Mvc.Controller);
} catch(e) {
	$e(e);
}